package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.core.enums.ProductTypeEnum;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.sys.bean.SysProductView;
import com.fzrskj.cloud.free.sys.dao.SysProductDao;
import com.fzrskj.cloud.free.user.bean.UserAccountViewAgentPro;
import com.fzrskj.cloud.free.user.dao.UserAccountDao;
import ms.core.tool.StrTool;
import org.apache.commons.collections.map.HashedMap;

import java.util.List;
import java.util.Map;

/**
 * SysProductAgentHelper.
 *
 * @author zj.
 *         Created on 2020/1/7 0007.
 */
public class SysProductAgentHelper {
    /**
     * 强制更新下级代理商产品说明.
     * @param productTypeEnum
     * @param productCode
     */
    public static void mustUpdateProductDetail(ProductTypeEnum productTypeEnum, String productCode) {
        mustUpdateProductDetail(productTypeEnum, productCode, null);
    }

    /**
     * 强制更新下级代理商产品说明.
     * @param productTypeEnum
     * @param productCode
     * @param subClass
     */
    public static void mustUpdateProductDetail(ProductTypeEnum productTypeEnum, String productCode, String subClass) {
        //如果不是顶级平台，不具本产品参数强行同步功能
        if (!CloudContextUtils.DEF_AGENT_ID.equals(CloudContextUtils.getAgentId())) {
            return;
        }

        SysProductUtils productUtils = new SysProductUtils();
        Map<String, Object> cdt = new HashedMap();
        cdt.put("productClass", productTypeEnum.getTag());
        cdt.put("productCode", productCode);
        if (!StrTool.isBlankStr(subClass)) {
            cdt.put("subClass", subClass);
        }
        SysProductView productView = productUtils.querySysProduct(cdt);

        if (null == productView) {
            return;
        }

        SysProductDao productDao = new SysProductDao();
        productDao.mustUpdateProductDetail(productTypeEnum, productCode, subClass, productView.getProductName(),
                productView.getProductDetail(), productView.getStatus(), productView.getRecommend());
    }

    /**
     * 强制同步删除.
     * @param productClass
     * @param productCode
     * @param subClass
     */
    public static void mustDelPro(String productClass, String productCode, String subClass) {
        SysProductDao productDao = new SysProductDao();
        productDao.mustDelPro(productClass, productCode, subClass);
    }

    /**
     * 添加产品.
     * @param productClass
     * @param productCode
     * @param subClass
     */
    public static void mustAddPro(String productClass, String productCode, String subClass) {
        //先查出所有代理
        Map<String, Object> cdt = new HashedMap();
        cdt.put("isAgent", "Y");

        UserAccountDao accountDao = new UserAccountDao();
        List<UserAccountViewAgentPro> accountList = accountDao.getAllSyncProAgentAccount();

        if (null == accountList) {
            return;
        }

        SysProductDao productDao = new SysProductDao();
        for (UserAccountViewAgentPro account : accountList) {
            productDao.mustAddPro(account.getUserId(), productClass, productCode, subClass, null == account.getDiyPremium() ? 20 : account.getDiyPremium() );
        }
    }

    /**
     * 代理同步产品说明.
     */
    public static void agentUpdateProductDetailAll() {
        //如果顶级平台，此功能
        if (CloudContextUtils.DEF_AGENT_ID.equals(CloudContextUtils.getAgentId())) {
            return;
        }
        SysProductDao productDao = new SysProductDao();
        productDao.agentUpdateProductDetailAll();
    }
}
